package com.ruoyi.system.module.deptSynchron.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.nacos.shaded.com.google.gson.*;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.Qualification;
import com.ruoyi.system.domain.SysUserRole;

import com.ruoyi.system.hbos.domain.HbosPersonnelInput;
import com.ruoyi.system.hbos.tool.HbosDataUtils;
import com.ruoyi.system.module.deptSynchron.service.IDeptSyncService;
import com.ruoyi.system.module.deptSynchron.service.IUserSyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/wtc/user")
public class UserController extends BaseController {
    @Autowired
    private IUserSyncService userSyncService;
    @Autowired
    private IDeptSyncService deptSyncService;

    // 将 JsonElement 转换为 List 对象
    private static <T> List<T> jsonElementToList(JsonElement jsonElement, Class<T> clazz) {
        List<T> list = new ArrayList<>();

        if (jsonElement.isJsonArray()) {
            JsonArray jsonArray = jsonElement.getAsJsonArray();
            Gson gson = new Gson();

            for (JsonElement element : jsonArray) {
                T item = gson.fromJson(element, clazz);
                list.add(item);
            }
        }

        return list;
    }

    @PostMapping("/synchron")
    public void dataUpdates() {
        HbosPersonnelInput hbosPersonnelInput = new HbosPersonnelInput();
        hbosPersonnelInput.setRelationType(101);
        hbosPersonnelInput.setStatus(1);
        // 获取人员user_name
        List<String> usernameList = userSyncService.selectUsernameList();

        HbosDataUtils utils = new HbosDataUtils();
        JSONObject json = utils.getHbosPersonnel(hbosPersonnelInput);

        if (!(json == null || json.equals(""))) {
            String code = json.getString("code");
            if (!(code == null || code.equals(""))) {
                if (code.equals("200")) {
                    //System.out.println(json.getJSONArray("data"));
                    //System.out.println(emailList);
                    JSONArray dataArray = json.getJSONArray("data");
                    for (int i = 0; i < dataArray.size(); i++) {
                        JsonParser jsonParser = new JsonParser();
                        JsonObject dataJson = jsonParser.parse(dataArray.get(i) + "").getAsJsonObject();

                        String username;
                        if (dataJson.get("jobNumber") == null) {
                            username = dataJson.get("staffId").getAsString();
                        } else {
                            username = dataJson.get("jobNumber").getAsString();
                        }

                        if (!usernameList.contains(username)) {
                            SysUser user = new SysUser();
                            user.setUserName(username);
                            user.setNickName(dataJson.get("name").getAsString());
                            if (dataJson.get("telephone") != null) {
                                user.setPhonenumber(dataJson.get("telephone").getAsString());
                            }
                            if (dataJson.get("genderCode") == null) {
                                user.setSex("2");
                            } else {
                                if (dataJson.get("genderCode").getAsString().endsWith("1")) {
                                    user.setSex("0");
                                } else if (dataJson.get("genderCode").getAsString().endsWith("2")) {
                                    user.setSex("1");
                                } else {
                                    user.setSex("2");
                                }
                            }
                            user.setPassword("$2a$10$QVzVLoDC5YLFZeE0Cip6vu4Aq9AimOIcQeA6P/2JCCWaa/NULhvya");
                            if (dataJson.get("businessDeptId") == null) {
                                user.setDeptId(100L);
                            } else {
                                if(deptSyncService.selectIdByEmail(dataJson.get("businessDeptId").getAsString())==null){
                                    user.setDeptId(100L);
                                }
                                else{
                                    user.setDeptId(deptSyncService.selectIdByEmail(dataJson.get("businessDeptId").getAsString()));
                                }

                            }
                            SysUserRole role = new SysUserRole();
                            userSyncService.insertUser(user);
                            JsonElement jsonElement=dataJson.get("qualifications");
                            List<Qualification> qualifications = jsonElementToList(jsonElement, Qualification.class);
                            if(qualifications.size()>0){
                                if(qualifications.get(qualifications.size()-1).getProfessionalTitleName().startsWith("主任")){
                                    role.setRoleId(4L);
                                }else if(qualifications.get(qualifications.size()-1).getProfessionalTitleName().startsWith("副主任")){
                                    role.setRoleId(5L);
                                }else{
                                    role.setRoleId(3L);
                                }
                            }else{
                                role.setRoleId(3L);
                            }

                            role.setUserId(user.getUserId());

                            userSyncService.insertUserRole(role);
                        } else {
                            System.out.println("更新");
                            SysUser user = new SysUser();
                            user.setUserName(username);
                            user.setNickName(dataJson.get("name").getAsString());
                            if (dataJson.get("telephone") != null) {
                                user.setPhonenumber(dataJson.get("telephone").getAsString());
                            }
                            if (dataJson.get("genderCode") == null) {
                                user.setSex("2");
                            } else {
                                if (dataJson.get("genderCode").getAsString().endsWith("1")) {
                                    user.setSex("0");
                                } else if (dataJson.get("genderCode").getAsString().endsWith("2")) {
                                    user.setSex("1");
                                } else {
                                    user.setSex("2");
                                }
                            }
                            if (dataJson.get("personnelDeptId") == null) {
                                user.setDeptId(100L);
                            } else {
                                if(deptSyncService.selectIdByEmail(dataJson.get("businessDeptId").getAsString())==null){
                                    user.setDeptId(100L);
                                }
                                else{
                                    user.setDeptId(deptSyncService.selectIdByEmail(dataJson.get("businessDeptId").getAsString()));
                                }
                            }
                            userSyncService.updateUserByUserName(user);
                        }
                    }
                }
            }
        }
    }
}
